package com.salesys.gr.gui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.LoginedUserHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.tony.service.UserService;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;

public class FrmMain2 {

	protected Shell FrmMain2;

	/**
	 * Open the window.
	 * 
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmMain2.open();
		FrmMain2.layout();
		while (!FrmMain2.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		if (FrmMain2.isDisposed()) {
			new UserService().isLogined("0", LoginedUserHelper.getLoginedUserUsername());
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		FrmMain2 = new Shell(SWT.CLOSE | SWT.MIN);
		FrmMain2.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				boolean result = new MsgBox().questionTip(FrmMain2, "确定要退出程序吗？");
				e.doit = result;
			}
		});
		FrmMain2.setSize(300, 200);
		FrmMain2.setText("主界面");
		// 窗口居中
		FrmMain2.setLocation(new FrmHelper().setLocationCenter(FrmMain2));

		Label lblTip = new Label(FrmMain2, SWT.NONE);
		lblTip.setBounds(30, 20, 160, 17);
		lblTip.setText("欢迎使用黑色素加盟销售系统");

		Label lblNickname = new Label(FrmMain2, SWT.NONE);
		lblNickname.setBounds(30, 50, 61, 17);
		lblNickname.setText("姓　　名：");

		Label lblMark = new Label(FrmMain2, SWT.NONE);
		lblMark.setBounds(30, 80, 61, 17);
		lblMark.setText("现　　金：");

		Label lblChildNum = new Label(FrmMain2, SWT.NONE);
		lblChildNum.setBounds(30, 110, 61, 17);
		lblChildNum.setText("下级个数：");

		final Label lblViewNickname = new Label(FrmMain2, SWT.NONE);
		lblViewNickname.setBounds(100, 50, 120, 17);

		final Label lblViewMark = new Label(FrmMain2, SWT.NONE);
		lblViewMark.setBounds(100, 80, 120, 17);

		final Label lblViewChildNum = new Label(FrmMain2, SWT.NONE);
		lblViewChildNum.setBounds(100, 110, 120, 17);

		Menu menu = new Menu(FrmMain2, SWT.BAR);
		FrmMain2.setMenuBar(menu);

		MenuItem mntm_p = new MenuItem(menu, SWT.CASCADE);
		mntm_p.setText("资料(&P)");

		Menu menu_p = new Menu(mntm_p);
		mntm_p.setMenu(menu_p);

		lblViewMark.setText(LoginedUserHelper.getLoginedUserMark());

		if (LoginedUserHelper.getLoginedUserNickname() != null) {
			lblViewNickname.setText(LoginedUserHelper.getLoginedUserNickname());
		} else {
			lblViewNickname.setText("");
		}

		try {
			if (LoginedUserHelper.getLoginedUserChild() != null) {
				lblViewChildNum.setText(new UserService().getChildNum(LoginedUserHelper.getLoginedUserChild()) + "");
			} else {
				lblViewChildNum.setText("0");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 查看个人资料
		MenuItem mntm_p_v = new MenuItem(menu_p, SWT.NONE);
		mntm_p_v.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmViewInfo().open();
			}
		});
		mntm_p_v.setText("查看个人资料(&V)");

		// 修改个人资料
		MenuItem mntm_p_m = new MenuItem(menu_p, SWT.NONE);
		mntm_p_m.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmModifyInfo1().open();

				LoginedUserHelper.fresh();
				lblViewMark.setText(LoginedUserHelper.getLoginedUserMark());

				if (LoginedUserHelper.getLoginedUserNickname() != null) {
					lblViewNickname.setText(LoginedUserHelper.getLoginedUserNickname());
				} else {
					lblViewNickname.setText("");
				}

				try {
					if (LoginedUserHelper.getLoginedUserChild() != null) {
						lblViewChildNum.setText(new UserService().getChildNum(LoginedUserHelper.getLoginedUserChild()) + "");
					} else {
						lblViewChildNum.setText("0");
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		mntm_p_m.setText("修改个人资料(&M)");

		MenuItem mntm_l_1 = new MenuItem(menu_p, SWT.SEPARATOR);
		mntm_l_1.setText("");

		// 修改密码
		MenuItem mntm_p_p = new MenuItem(menu_p, SWT.NONE);
		mntm_p_p.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmModifyPwd().open();
			}
		});
		mntm_p_p.setText("修改密码(&P)");

		MenuItem mntm_m = new MenuItem(menu, SWT.CASCADE);
		mntm_m.setText("管理(&M)");

		Menu menu_m = new Menu(mntm_m);
		mntm_m.setMenu(menu_m);

		// 注册用户
		MenuItem mntm_m_r = new MenuItem(menu_m, SWT.NONE);
		mntm_m_r.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmReg().open();

				LoginedUserHelper.fresh();
				lblViewMark.setText(LoginedUserHelper.getLoginedUserMark());

				if (LoginedUserHelper.getLoginedUserNickname() != null) {
					lblViewNickname.setText(LoginedUserHelper.getLoginedUserNickname());
				} else {
					lblViewNickname.setText("");
				}

				try {
					if (LoginedUserHelper.getLoginedUserChild() != null) {
						lblViewChildNum.setText(new UserService().getChildNum(LoginedUserHelper.getLoginedUserChild()) + "");
					} else {
						lblViewChildNum.setText("0");
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		mntm_m_r.setText("注册新用户(&R)");

		// 查看关系
		MenuItem mntm_m_v = new MenuItem(menu_m, SWT.NONE);
		mntm_m_v.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmViewRelation1().open();
			}
		});
		mntm_m_v.setText("查看用户关系(&V)");

		// 查看积分纪录
		MenuItem mntm_m_m = new MenuItem(menu_m, SWT.NONE);
		mntm_m_m.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FrmViewMarkRecord FrmViewMarkRecord1 = new FrmViewMarkRecord();
				FrmViewMarkRecord1.transfer(LoginedUserHelper.getLoginedUserId());
				FrmViewMarkRecord1.open();
			}
		});
		mntm_m_m.setText("查看现金纪录(&M)");

		// 分割线
		MenuItem menuItem1 = new MenuItem(menu_m, SWT.SEPARATOR);
		menuItem1.setText("");

		// 处理体现申请
		MenuItem mntm_m_w = new MenuItem(menu_m, SWT.NONE);
		mntm_m_w.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmHandleWithdraw().open();
			}
		});
		mntm_m_w.setText("处理提现申请(&W)");

		// 处理密码重置申请
		MenuItem mntm_m_h = new MenuItem(menu_m, SWT.NONE);
		mntm_m_h.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmHandlePSWReset().open();
			}
		});
		mntm_m_h.setText("处理密码重置申请(&H)");

		// 分割线
		MenuItem menuItem2 = new MenuItem(menu_m, SWT.SEPARATOR);
		menuItem2.setText("");

		// 修改积分比率
		MenuItem mntm_m_c = new MenuItem(menu_m, SWT.NONE);
		mntm_m_c.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmModifyConver().open();
			}
		});
		mntm_m_c.setText("修改积分比率(&C)");

		// 管理会员
		MenuItem mntm_m_g = new MenuItem(menu_m, SWT.NONE);
		mntm_m_g.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmManageVIPs().open();

				LoginedUserHelper.fresh();
				lblViewMark.setText(LoginedUserHelper.getLoginedUserMark());

				if (LoginedUserHelper.getLoginedUserNickname() != null) {
					lblViewNickname.setText(LoginedUserHelper.getLoginedUserNickname());
				} else {
					lblViewNickname.setText("");
				}

				try {
					if (LoginedUserHelper.getLoginedUserChild() != null) {
						lblViewChildNum.setText(new UserService().getChildNum(LoginedUserHelper.getLoginedUserChild()) + "");
					} else {
						lblViewChildNum.setText("0");
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		mntm_m_g.setText("管理用户(&G)");

		MenuItem mntm_h = new MenuItem(menu, SWT.CASCADE);
		mntm_h.setText("帮助(&H)");

		Menu menu_h = new Menu(mntm_h);
		mntm_h.setMenu(menu_h);

		// 关于
		MenuItem mntm_h_a = new MenuItem(menu_h, SWT.NONE);
		mntm_h_a.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new FrmAbout().open();
			}
		});
		mntm_h_a.setText("关于(&A)");

		// long s = (new Date()).getTime();
		// for (int count = 1;; count++) {
		// if (new Date().getTime() == count * 5000 + s) {
		// for (int id = 1;; id++) {
		// Reset reset = new UserDao().queryPSWReset(id);
		// if (reset.getUsername() == null) {
		// break;
		// } else {
		// new FrmHandlePSWReset().open();
		// }
		// }
		// }
		// }

	}
}
